﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ilibrary.source.common
{
    public class Common
    {
        public static char[] seps = { '.', ' ', '-', '_', '(', ')', '{', '}', '[', ']', ',', ';', ':', '?', '!' };

        /// <summary>
        /// expand xml reps for reserved characters
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static String expandTags(String xml)
        {
            if (xml == null)
                return null;
            else
            {
                xml = xml.Replace("&lt;", "<");
                xml = xml.Replace("&gt;", ">");
                xml = xml.Replace("&amp;", "&");
                return xml;


            }
        }


        /// <summary>
        /// Replace all reserved characters in an xml fragment
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public static string normalizeXML(string buf)
        {
            string name = buf.Replace("<", "&lt;");
            name = name.Replace(">", "&gt;");
            name = name.Replace("&", "&amp;");

            return name;
        }

        /// <summary>
        /// Replaces all single quotes in string with double quotes
        /// in order to avoird SQL syntax errors
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public static string QuoteEscape(string buf)
        {
            if (string.IsNullOrEmpty(buf))
                return buf;
            return buf.Replace("'", "''");
        }

        public static string transformTocToHtml(string toc)
        {
            toc = toc.Replace("<toc>", "<ul class=\"tocUnit\">");
            toc = toc.Replace("</toc>", "</ul>");
            toc = toc.Replace("<tocItem>", "<li class=\"tocItem\">");
            toc = toc.Replace("</tocItem>", "</li>");
            toc = toc.Replace("<topic>", "<span class=\"topic\">");
            toc = toc.Replace("</topic>", "</span>");
            return toc;

        }

        public static string humanReadable(long size)
        {
            double tot;
            if (size >= 1024 * 1024 * 1024)
            {
                tot = 1024.0 * 1024.0 * 1024.0;
                double tmp = (size / tot);

                return tmp.ToString("F") + " Go";
            }
            else if (size >= 1024 * 1024)
            {
                tot = 1024.0 * 1024.0;
                double tmp = (size / tot);

                return tmp.ToString("F") + " Mo";

            }
            else if (size >= 1024)
            {
                double tmp = (size / 1024.0);

                return tmp.ToString("F") + " Ko";
            }
            else return size + " O";
        }

    }
}
